/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.ai2.handler;

import com.aionengine.gameserver.ai2.AIState;
import com.aionengine.gameserver.ai2.AISubState;
import com.aionengine.gameserver.ai2.NpcAI2;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
import com.aionengine.gameserver.questEngine.QuestEngine;
import com.aionengine.gameserver.questEngine.model.QuestEnv;
import com.aionengine.gameserver.services.TownService;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author ATracer
 */
public class TalkEventHandler {

    /**
     * @param npcAI
     * @param creature
     */
    public static void onTalk(NpcAI2 npcAI, Creature creature) {
        onSimpleTalk(npcAI, creature);

        if (creature instanceof Player) {
            Player player = (Player) creature;
            if (QuestEngine.getInstance().onDialog(new QuestEnv(npcAI.getOwner(), player, 0, -1)))
                return;
            // only player villagers can use villager npcs in oriel/pernon
            switch (npcAI.getOwner().getObjectTemplate().getTitleId()) {
                case 462877:
                    int playerTownId = TownService.getInstance().getTownResidence(player);
                    int currentTownId = TownService.getInstance().getTownIdByPosition(player);
                    if (playerTownId != currentTownId) {
                        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(npcAI.getOwner().getObjectId(), 44));
                        return;
                    } else {
                        PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(npcAI.getOwner().getObjectId(), 10));
                        return;
                    }
                default:
                    PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(npcAI.getOwner().getObjectId(), 10));
                    break;
            }
        }

    }

    /**
     * @param npcAI
     * @param creature
     */
    public static void onSimpleTalk(NpcAI2 npcAI, Creature creature) {
        if (npcAI.getOwner().getObjectTemplate().isDialogNpc()) {
            npcAI.setSubStateIfNot(AISubState.TALK);
            npcAI.getOwner().setTarget(creature);
        }
    }

    /**
     * @param npcAI
     * @param creature
     */
    public static void onFinishTalk(NpcAI2 npcAI, Creature creature) {
        Npc owner = npcAI.getOwner();
        if (owner.isTargeting(creature.getObjectId())) {
            if (npcAI.getState() != AIState.FOLLOWING)
                owner.setTarget(null);
            npcAI.think();
        }
    }

    /**
     * No SM_LOOKATOBJECT broadcast
     *
     * @param npcAI
     * @param creature
     */
    public static void onSimpleFinishTalk(NpcAI2 npcAI, Creature creature) {
        Npc owner = npcAI.getOwner();
        if (owner.isTargeting(creature.getObjectId()) && npcAI.setSubStateIfNot(AISubState.NONE)) {
            owner.setTarget(null);
        }
    }

}
